package xjh.him.netty.codec;


import xjh.him.common.ErrorMsg;
import xjh.him.netty.dto.ChatDto;
import xjh.him.netty.dto.ChatMsgDto;
import xjh.him.netty.dto.FriendDto;
import xjh.him.netty.dto.HeartBeat;
import xjh.him.netty.dto.UserDto;

public class Command {
    public static final byte Error = 0;

    public static final byte HEART_BEAT = 100;

    // user
    public static final byte REGISTER_REQ = 1;
    public static final byte REGISTER_RESP = 2;
    public static final byte LOGIN_REQ = 3;
    public static final byte LOGIN_RESP = 4;
    public static final byte SELECT_USER_BY_USER_ID_REQ = 14;
    public static final byte SELECT_USER_BY_USER_ID_RESP = 15;
    public static final byte SELECT_USER_BY_PHONE_REQ = 19;
    public static final byte SELECT_USER_BY_PHONE_RESP = 20;

    // friend
    public static final byte SELECT_FRIEND_REQ = 5;
    public static final byte SELECT_FRIEND_RESP = 6;
    public static final byte APPLY_FOR_FRIEND_REQ = 21;
    public static final byte APPLY_FOR_FRIEND_RESP = 22;
    public static final byte HANDLE_APPLY_REQ = 23;
    public static final byte HANDLE_APPLY_RESP = 24;
    public static final byte SELECT_COUNT_APPLY_REQ = 25;
    public static final byte SELECT_COUNT_APPLY_RESP = 26;
    public static final byte SELECT_APPLY_INFO_REP = 27;
    public static final byte SELECT_APPLY_INFO_RESP = 28;

    // chat
    public static final byte INSERT_CHAT_REQ = 7;
    public static final byte INSERT_CHAT_RESP = 8;
    public static final byte SELECT_CHAT_BY_USER_ID_REQ = 9;
    public static final byte SELECT_CHAT_BY_USER_ID_RESP = 10;

    // chat_msg
    public static final byte INSERT_CHAT_MSG_REQ = 11;
    public static final byte SELECT_PAGE_CHAT_MSG_REQ = 12;
    public static final byte SELECT_PAGE_CHAT_MSG_RESP = 13;
    public static final byte NOTIFY_MSG = 16;
    public static final byte FILE_UPLOAD_REQ = 17;
    public static final byte FILE_UPLOAD_RESP = 18;

    public static Class<?> getClazz(byte command) {
        Class<?> clazz;
        switch (command) {
            case Error:
                clazz = ErrorMsg.class;
                break;
            case REGISTER_REQ:
                clazz = UserDto.RegisterReq.class;
                break;
            case REGISTER_RESP:
                clazz = UserDto.RegisterResp.class;
                break;
            case LOGIN_REQ:
                clazz = UserDto.LoginReq.class;
                break;
            case LOGIN_RESP:
                clazz = UserDto.LoginResp.class;
                break;
            case SELECT_FRIEND_REQ:
                clazz = FriendDto.SelectFriendsReq.class;
                break;
            case SELECT_FRIEND_RESP:
                clazz = FriendDto.SelectFriendsResp.class;
                break;
            case INSERT_CHAT_REQ:
                clazz = ChatDto.InsertReq.class;
                break;
            case INSERT_CHAT_RESP:
                clazz = ChatDto.InsertResp.class;
                break;
            case SELECT_CHAT_BY_USER_ID_REQ:
                clazz = ChatDto.SelectByUserIdReq.class;
                break;
            case SELECT_CHAT_BY_USER_ID_RESP:
                clazz = ChatDto.SelectByUserIdResp.class;
                break;
            case INSERT_CHAT_MSG_REQ:
                clazz = ChatMsgDto.InsertReq.class;
                break;
            case SELECT_PAGE_CHAT_MSG_REQ:
                clazz = ChatMsgDto.SelectPageReq.class;
                break;
            case SELECT_PAGE_CHAT_MSG_RESP:
                clazz = ChatMsgDto.SelectPageResp.class;
                break;
            case SELECT_USER_BY_USER_ID_REQ:
                clazz = UserDto.SelectByUserIdReq.class;
                break;
            case SELECT_USER_BY_USER_ID_RESP:
                clazz = UserDto.SelectByUserIdResp.class;
                break;
            case NOTIFY_MSG:
                clazz = ChatMsgDto.NotifyMsg.class;
                break;
            case FILE_UPLOAD_REQ:
                clazz = ChatMsgDto.FileUploadReq.class;
                break;
            case FILE_UPLOAD_RESP:
                clazz = ChatMsgDto.FileUploadResp.class;
                break;
            case SELECT_USER_BY_PHONE_REQ:
                clazz = UserDto.SelectByPhoneReq.class;
                break;
            case SELECT_USER_BY_PHONE_RESP:
                clazz = UserDto.SelectByPhoneResp.class;
                break;
            case APPLY_FOR_FRIEND_REQ:
                clazz = FriendDto.ApplyForFriendReq.class;
                break;
            case APPLY_FOR_FRIEND_RESP:
                clazz = FriendDto.ApplyForFriendResp.class;
                break;
            case HANDLE_APPLY_REQ:
                clazz = FriendDto.HandleApplyReq.class;
                break;
            case HANDLE_APPLY_RESP:
                clazz = FriendDto.HandleApplyResp.class;
                break;
            case SELECT_COUNT_APPLY_REQ:
                clazz = FriendDto.SelectCountApplyReq.class;
                break;
            case SELECT_COUNT_APPLY_RESP:
                clazz = FriendDto.SelectCountApplyResp.class;
                break;
            case SELECT_APPLY_INFO_REP:
                clazz = FriendDto.SelectApplyInfoReq.class;
                break;
            case SELECT_APPLY_INFO_RESP:
                clazz = FriendDto.SelectApplyInfoResp.class;
                break;
            case HEART_BEAT:
                clazz = HeartBeat.class;
                break;
            default:
                throw new IllegalStateException("unknown command: " + command);
        }
        return clazz;
    }

    public static byte getCommand(Class<?> clazz) {
        byte command;
        if (clazz == ErrorMsg.class) {
            command = Error;
        }else if (clazz == UserDto.RegisterReq.class) {
            command = REGISTER_REQ;
        }else if (clazz == UserDto.RegisterResp.class) {
            command = REGISTER_RESP;
        }else if (clazz == UserDto.LoginReq.class) {
            command = LOGIN_REQ;
        }else if (clazz == UserDto.LoginResp.class) {
            command = LOGIN_RESP;
        }else if (clazz == FriendDto.SelectFriendsReq.class) {
            command = SELECT_FRIEND_REQ;
        }else if (clazz == FriendDto.SelectFriendsResp.class) {
            command = SELECT_FRIEND_RESP;
        }else if (clazz == ChatDto.InsertReq.class) {
            command = INSERT_CHAT_REQ;
        }else if (clazz == ChatDto.InsertResp.class) {
            command = INSERT_CHAT_RESP;
        }else if (clazz == ChatDto.SelectByUserIdReq.class) {
            command = SELECT_CHAT_BY_USER_ID_REQ;
        }else if (clazz == ChatDto.SelectByUserIdResp.class) {
            command = SELECT_CHAT_BY_USER_ID_RESP;
        }else if (clazz == ChatMsgDto.InsertReq.class) {
            command = INSERT_CHAT_MSG_REQ;
        }else if (clazz == ChatMsgDto.SelectPageReq.class) {
            command = SELECT_PAGE_CHAT_MSG_REQ;
        }else if (clazz == ChatMsgDto.SelectPageResp.class) {
            command = SELECT_PAGE_CHAT_MSG_RESP;
        }else if (clazz == UserDto.SelectByUserIdReq.class) {
            command = SELECT_USER_BY_USER_ID_REQ;
        }else if (clazz == UserDto.SelectByUserIdResp.class) {
            command = SELECT_USER_BY_USER_ID_RESP;
        }else if (clazz == ChatMsgDto.NotifyMsg.class) {
            command = NOTIFY_MSG;
        }else if (clazz == ChatMsgDto.FileUploadReq.class) {
            command = FILE_UPLOAD_REQ;
        }else if (clazz == ChatMsgDto.FileUploadResp.class) {
            command = FILE_UPLOAD_RESP;
        }else if (clazz == UserDto.SelectByPhoneReq.class) {
            command = SELECT_USER_BY_PHONE_REQ;
        }else if (clazz == UserDto.SelectByPhoneResp.class) {
            command = SELECT_USER_BY_PHONE_RESP;
        }else if (clazz == FriendDto.ApplyForFriendReq.class) {
            command = APPLY_FOR_FRIEND_REQ;
        }else if (clazz == FriendDto.ApplyForFriendResp.class) {
            command = APPLY_FOR_FRIEND_RESP;
        }else if (clazz == FriendDto.HandleApplyReq.class) {
            command = HANDLE_APPLY_REQ;
        }else if (clazz == FriendDto.HandleApplyResp.class) {
            command = HANDLE_APPLY_RESP;
        }else if (clazz == FriendDto.SelectCountApplyReq.class) {
            command = SELECT_COUNT_APPLY_REQ;
        }else if (clazz == FriendDto.SelectCountApplyResp.class) {
            command = SELECT_COUNT_APPLY_RESP;
        }else if (clazz == FriendDto.SelectApplyInfoReq.class) {
            command = SELECT_APPLY_INFO_REP;
        }else if (clazz == FriendDto.SelectApplyInfoResp.class) {
            command = SELECT_APPLY_INFO_RESP;
        }else if (clazz == HeartBeat.class) {
            command = HEART_BEAT;
        }
        else {
            throw new IllegalStateException("unknown clazz: " + clazz);
        }
        return command;
    }
}
